package gui2;

import domain.GameManager;
import domain.Move;
import domain.Square;
import domain.UserInputQueue;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class OthelloUI extends JFrame{  
    
    private GameManager gm;
    
    private int player1type;
    private int player2type;
    private int player1AI;
    private int player2AI;
    private int debugMode; 
    private int trainingMode;
    private int[][] grid;
    
    private UserInputQueue queue = new UserInputQueue();
    
    private OutputWindow outputWindow;
    
    private ChooseBotDifficulty botDifficulty;
    
    public int lastClickedX;
    public int lastClickedY;
   
    // New game started etc. i output  
    
    //The constructor which takes the width and height of the window
    public OthelloUI(int width, int height) {        
        this.setSize(width, height);
        this.setTitle("Othello is for team jakob!");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(400, 200);
        this.setResizable(false);
        
        //InitComponents(width, height);  
        GridLayout layout = new GridLayout(0,8);
        this.setLayout(layout);
        
        for(int i = 0; i < 8; i++){
            for(int j = 0; j < 8; j++){
                OthelloBrick brick = new OthelloBrick(j, i);
                BrickListener(brick);
                this.add(brick);
            }             
        }      
           botDifficulty = new ChooseBotDifficulty(450, 130);
           botDifficulty.btnOk.addActionListener(new ActionListener() {
           
               
            @Override
            public void actionPerformed(ActionEvent ae) {
                player1AI = botDifficulty.getaI1Level();
                player2AI = botDifficulty.getaI2Level();
                player1type = botDifficulty.getPlayer1Type();
                player2type = botDifficulty.getPlayer2Type();
                
                debugMode = botDifficulty.getDebugmode();
                trainingMode = botDifficulty.getTrainingMode();
                
                grid = botDifficulty.getGrid();
       
                botDifficulty.setVisible(false);
                setVisible(true);
                
                startUp();
            }
        });
    }
    
    public void startUp(){
        //kör setupgame
        //botDifficulty = new ChooseBotDifficulty(400, 400);
        //outputWindow = new OutputWindow(300, 300);
        
        System.out.println("here UI");
        //skapa gm
        gm = new GameManager(this, player1type, player2type, player1AI, player2AI, queue, debugMode, outputWindow, grid, trainingMode);
        
        //skapa board
        
        
        // starta tråd
        System.out.println("starting thread");
        Thread t = new Thread(gm);
        t.start();
    }
    
    public void rerun(){
        System.out.println("RERUN STARTED!");
        gm = new GameManager(this, player1type, player2type, player1AI, player2AI, queue, debugMode, outputWindow, grid, trainingMode);
        
        System.out.println("starting thread");
        Thread t = new Thread(gm);
        t.start();
    }

    public void BrickListener(final OthelloBrick brick)
    {
        brick.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent me) {
                Move move = new Move(brick.x,brick.y);
                lastClickedX = brick.x;
                lastClickedY = brick.y;
                
                queue.pushQueue(move);
            }

            @Override
            public void mousePressed(MouseEvent me) {
            }

            @Override
            public void mouseReleased(MouseEvent me) {
            }

            @Override
            public void mouseEntered(MouseEvent me) {
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }
        });
    }
    
    public boolean IsValidMove(int x, int y)
    {
        if(GetBrickOwner(x - 1, y + 1) != -1)
            return true;
        else if(GetBrickOwner(x, y + 1) != -1)
            return true;
        else if(GetBrickOwner(x + 1, y + 1) != -1)
            return true;
        else if(GetBrickOwner(x - 1, y) != -1)
            return true;
        else if(GetBrickOwner(x, y) != -1)
            return true;
        else if(GetBrickOwner(x + 1, y) != -1)
            return true;
        else if(GetBrickOwner(x - 1, y - 1) != -1)
            return true;
        else if(GetBrickOwner(x, y - 1) != -1)
            return true;
        else if(GetBrickOwner(x + 1, y - 1) != -1)
            return true;
        return false;
    }
   
    // Position is the position in the othello brick array (0 to 63)
    // Doesn't work if the color is green, use changetospecific color instead.
    public void changeBrickColor(int x, int y){
        int temp = y * 8 + x;
        
        JLayeredPane layeredPane = this.getLayeredPane();  
        JPanel jp = (JPanel)layeredPane.getComponent(0);
        
        ((OthelloBrick)jp.getComponent(temp)).changeColor();
    }
    
    public void changeBrickColorToSpecificColor(int x, int y, Color color){
        int temp = y * 8 + x;
        
        JLayeredPane layeredPane = this.getLayeredPane();  
        JPanel jp = (JPanel)layeredPane.getComponent(0);
        
        ((OthelloBrick)jp.getComponent(temp)).changeColorToSpecific(color);
    } 
    
    public int GetBrickOwner(int x, int y)
    {
        int temp = y * 8 + x;
        
        JLayeredPane layeredPane = this.getLayeredPane();  
        JPanel jp = (JPanel)layeredPane.getComponent(0);
        
        return ((OthelloBrick)jp.getComponent(temp)).GetOwner();
    }
        
    public void ResetBoard(){
        int temp;
        JLayeredPane layeredPane = this.getLayeredPane();  
        
        for(int y=0; y<8; y++){
            for(int x=0; x<8; x++){
                temp = y * 8 + x;        
                
                JPanel jp = (JPanel)layeredPane.getComponent(0);

                ((OthelloBrick)jp.getComponent(temp)).changeColorToSpecific(Color.green);
            }
        }
    }
    
    public void PrintToOutputScreen(String message) {
        outputWindow.PrintToScreen(message);
    }
    
    public void CleanOutputScreen() {
        outputWindow.CleanScreen();
    }
    
    public void UpdateUIBoard(Square[][] grid){
        //System.out.println("updateingUIgrid");
        for(int y=0; y<8; y++){
            for(int x=0; x<8; x++){
                switch(grid[x][y].getOwner())
                {
                    case 0: 
                        changeBrickColorToSpecificColor(x, y, Color.green);
                        break;
                    case 1: 
                        changeBrickColorToSpecificColor(x, y, Color.black);
                        break;
                    case 2: 
                        changeBrickColorToSpecificColor(x, y, Color.white);
                        break;
                }                
            }
        }
    }
}
